<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="路段id" prop="roadpartId">
        <el-input
          v-model="queryParams.roadpartId"
          placeholder="请输入路段id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="养护时间" prop="recordDate">
        <el-date-picker
          clearable
          v-model="queryParams.recordDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择养护时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="养护员工id" prop="staffId">
        <el-input
          v-model="queryParams.staffId"
          placeholder="请输入养护员工id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修类型" prop="sysFixType">
        <el-select
          v-model="queryParams.sysFixType"
          placeholder="请选择维修类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_fix_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="是否抽检" prop="checkr">
        <el-select v-model="queryParams.checkr" placeholder="请选择抽检类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_list_checker"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <!-- <el-button type="warning" size="small" @click="randomselect">随机抽检</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:recordlist:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:recordlist:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:recordlist:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:recordlist:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="recordlistList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="养护记录id" align="center" prop="recordId" />
      <el-table-column label="养护问题记录" align="center" prop="problem" />
      <el-table-column label="路段id" align="center" prop="roadpartId" />
      <el-table-column
        label="养护时间"
        align="center"
        prop="recordDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recordDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="养护员工id" align="center" prop="staffId" />
      <el-table-column label="维修类型" align="center" prop="sysFixType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_fix_type"
            :value="scope.row.sysFixType"
          />
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="picture">
        <template slot-scope="scope" width="90">
          <ImagePreview :width="50" :height="50" :src="scope.row.picture" />
        </template>
      </el-table-column>
      <el-table-column label="抽检状态" align="center" prop="checkr">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_list_checker" :value="scope.row.checkr"/>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="yusuan(scope.row)"
            >预算信息</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:recordlist:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:recordlist:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改养护管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="养护问题记录" prop="problem">
          <el-input v-model="form.problem" placeholder="请输入养护问题记录" />
        </el-form-item>
        <el-form-item label="路段id" prop="roadpartId">
          <el-input v-model="form.roadpartId" placeholder="请输入路段id" />
        </el-form-item>
        <el-form-item label="养护时间" prop="recordDate">
          <el-date-picker
            clearable
            v-model="form.recordDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择养护时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="养护员工" prop="staffId">
          <el-input v-model="form.staffId" placeholder="请输入养护员工id" />
        </el-form-item>
        <el-form-item label="维修类型" prop="sysFixType">
          <el-select v-model="form.sysFixType" placeholder="请选择维修类型">
            <el-option
              v-for="dict in dict.type.sys_fix_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片">
          <image-upload v-model="form.picture"/>
        </el-form-item>
        <!-- <el-form-item label="图片上传" prop="picture">
          <el-col :span="1.5">
            <el-upload ref="upload" accept=".jpg,.png" >
              <el-button
                type="primary"
                icon="el-icon-upload"
                size="mini"
                @click="handleAddpicture"
                >上传</el-button
              >
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-col>
        </el-form-item> -->
        <el-divider content-position="center">台账管理信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddStandingbook"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteStandingbook"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="standingbookList"
          :row-class-name="rowStandingbookIndex"
          @selection-change="handleStandingbookSelectionChange"
          ref="standingbook"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column
            label=""
            prop="safetyresponsibilityletter"
            width="150"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.safetyresponsibilityletter"
                placeholder="请输入"
              />
            </template>
          </el-table-column>
          <el-table-column label="" prop="roster" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.roster" placeholder="请输入" />
            </template>
          </el-table-column>
          <el-table-column label="" prop="devicemanagement" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.devicemanagement"
                placeholder="请输入"
              />
            </template>
          </el-table-column>
          <el-table-column label="" prop="laborprotectionsupplies" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.laborprotectionsupplies"
                placeholder="请输入"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRecordlist,
  getRecordlist,
  delRecordlist,
  addRecordlist,
  updateRecordlist,
} from "@/api/system/recordlist";

export default {
  name: "Recordlist",
  dicts: ["sys_fix_type",'sys_list_checker'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedStandingbook: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 养护管理表格数据
      recordlistList: [],
      // 台账管理表格数据
      standingbookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roadpartId: null,
        recordDate: null,
        staffId: null,
        sysFixType: null,
        checkr:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
    //上传参数
  },
  created() {
    if (this.$route.query.pushid) {
      var pId = this.$route.query.pushid;
      this.queryParams.roadpartId = pId;
    }
    this.getList();
  },

  methods: {
    /**跳转到预算信息 */
    yusuan(row) {
      this.$router.push({
        path: "/system/budget",
        query: { pushid: row.roadpartId },
      });
    },
    // /**随机抽检按钮操作 */
    //     randomselect(){
    //       var randomroadId=this.total;
    //       this.queryParams.roadpartId=parseInt(Math.random()*(randomroadId-1+1)+1);
    //       this.handleQuery;

    //     },

    /** 查询养护管理列表 */
    getList() {
      this.loading = true;
      listRecordlist(this.queryParams).then((response) => {
        this.recordlistList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        problem: null,
        roadpartId: null,
        recordDate: null,
        staffId: null,
        sysFixType: null,
        picture: null,
        checkr: null
      };
      this.standingbookList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.recordId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加养护管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids;
      getRecordlist(recordId).then((response) => {
        this.form = response.data;
        this.standingbookList = response.data.standingbookList;
        this.open = true;
        this.title = "修改养护管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.standingbookList = this.standingbookList;
          if (this.form.recordId != null) {
            updateRecordlist(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecordlist(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal
        .confirm('是否确认删除养护管理编号为"' + recordIds + '"的数据项？')
        .then(function () {
          return delRecordlist(recordIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 台账管理序号 */
    rowStandingbookIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 台账管理添加按钮操作 */
    handleAddStandingbook() {
      let obj = {};
      obj.safetyresponsibilityletter = "";
      obj.roster = "";
      obj.devicemanagement = "";
      obj.laborprotectionsupplies = "";
      this.standingbookList.push(obj);
    },
    /**添加图片操作 */
    handleAddpicture() {},
    /** 台账管理删除按钮操作 */
    handleDeleteStandingbook() {
      if (this.checkedStandingbook.length == 0) {
        this.$modal.msgError("请先选择要删除的台账管理数据");
      } else {
        const standingbookList = this.standingbookList;
        const checkedStandingbook = this.checkedStandingbook;
        this.standingbookList = standingbookList.filter(function (item) {
          return checkedStandingbook.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleStandingbookSelectionChange(selection) {
      this.checkedStandingbook = selection.map((item) => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/recordlist/export",
        {
          ...this.queryParams,
        },
        `recordlist_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
